﻿$(function() {
    createWatermarks();
    createTooltips();

    $('.jq_openid').openidpickr();
    $('.jq_registerCancel').click(jq_registerCancel_click);
    $('.jq_registerOK').click(jq_registerOK_click);
    $('.jq_register').click(jq_register_click);
    $('.jq_login').click(jq_login_click);
});

function createWatermarks() {
    $('.jq_username').Watermark('Username');
    $('.jq_password').Watermark('Password');
    $('.jq_openid_username').Watermark('Username');
    $('.jq_registerUsername').Watermark('Username');
    $('.jq_registerFirstName').Watermark('First Name');
    $('.jq_registerLastName').Watermark('Last Name');
    $('.jq_registerEmail').Watermark('Email');
    $('.jq_registerEmailConfirm').Watermark('Confirm');
    $('.jq_registerPassword').Watermark('Password');
    $('.jq_registerPasswordConfirm').Watermark('Confirm');
}

function createTooltips() {
    var options = {
        show: 'mouseover',
        hide: 'mouseout',
        style: {
            padding: 2,
            background: '#3399CC',
            color: 'white',
            tip: 'topMiddle',
            border: {
                color: '#3399CC'
            }
        },
        position: {
            corner: {
                target: 'bottomMiddle',
                tooltip: 'topMiddle'
            }
        }
    };

    if (!$.browser.msie) {
        options.style.border.width = 5;
        options.style.border.radius = 3;
    }

    options.content = 'OpenID is an open, decentralized standard for user authentication and access control, allowing users to log onto many services with the same digital identity. ';
    options.style.width = 200;
    $('#whatIsOpenId').qtip(options);
}

function jq_registerCancel_click(event) {
    $('#register').slideUp();
    $('#signIn').slideDown();
    return false;
}

function jq_register_click(event) {
    $('#signIn').slideUp();
    $('#register').slideDown();
    return false;
}

function jq_login_click() {
    var username = $('.jq_username').val();
    var password = $('.jq_password').val();

    authenticate(username, password);
    return false;
}

function authenticate(username, password) {
    $.getJSON("/Authentication/Authenticate/" + username + '/' + password, authCallback);
}

function authCallback(result) {
    if (result.Success) {
        location.reload(true);
    }
}

function jq_registerOK_click() {
    var valid = validDateAccountCreation();

    if (valid) {
        var username = $('.jq_registerUsername').val();
        var password = $('.jq_registerPassword').val();
        var email = $('.jq_registerEmail').val();
        var firstName = $('.jq_registerFirstName').val();
        var lastName = $('.jq_registerLastName').val();

        createAccount(username, password, email, firstName, lastName);
    }
}

function createAccount(username, password, email, firstName, lastName) {
    var url = '/Authentication/CreateAccount/' + username + '/' + firstName 
        + '/' + lastName + '/' + email + '/' + password;

    $.getJSON(url,createAccount_callback)
}

function createAccount_callback(response) {
    if (response.Success) {
        location.reload(true);
    } else {
        $.jGrowl(response.Message);
    }
}
        
        
        